package com.alexsun.newslist

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity(), OnItemClickListener {
    private var listView: ListView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        listView = findViewById(R.id.listItems)
        val adapter = NewsArrayAdapter(this, R.layout.news_item, getNewsList())
        listView!!.adapter = adapter

        listView!!.onItemClickListener = this
    }

    private fun getNewsList(): List<NewsItem> {
        val list = ArrayList<NewsItem>()
        for (i in 1..30) {
            val item = NewsItem("新闻$i")
            list.add(item)
        }
        return list
    }

    override fun onItemClick(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
        Toast.makeText(this, "点击了 $p2 的新闻", Toast.LENGTH_SHORT).show()
        val intent = Intent()
        val bundle = Bundle()
        val textView: TextView = p1!!.findViewById(R.id.item_text_view)
        bundle.putString("info", textView.text.toString())
        intent.putExtra("list", bundle)
        intent.setClass(this@MainActivity, NewsDetail::class.java)
        startActivity(intent)
    }
}